from PyQt6.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QStackedWidget,
                             QListWidget, QListWidgetItem, QLabel, QApplication)
from PyQt6.QtCore import Qt, QSize
from PyQt6.QtGui import QIcon
from qfluentwidgets import (PushButton, CardWidget, FluentIcon as FIF,
                            ScrollArea, InfoBar, PrimaryPushButton,
                            StrongBodyLabel, BodyLabel, ListWidget)
from exampaper import ExamPaperWindow
from upload import UploadWidget
from profile import ProfileWidget

class DashboardWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.current_page = 1
        self.items_per_page = 5  # 每页显示5套试卷
        self.total_papers = 20   # 总共20套试卷
        self.setup_ui()
        
    def setup_ui(self):
        self.setWindowTitle("智能练习系统")
        self.resize(1200, 800)
        
        # 主布局
        layout = QHBoxLayout(self)
        
        # 左侧功能列表
        left_card = CardWidget()
        left_layout = QVBoxLayout(left_card)
        
        # 创建功能列表
        self.menu_list = ListWidget(self)
        
        # 添加功能项
        self.add_menu_item("考题练习", FIF.BROOM.icon())
        self.add_menu_item("上传习题", FIF.BROOM.icon())
        self.add_menu_item("个人中心", FIF.PEOPLE.icon())
        
        left_layout.addWidget(self.menu_list)
        left_card.setMaximumWidth(250)
        
        # 右侧内容区域
        self.content_stack = QStackedWidget()
        
        # 添加考题练习页面
        self.exercise_page = self.create_exercise_page()
        self.content_stack.addWidget(self.exercise_page)
        
        # 添加上传习题页面
        self.upload_page = UploadWidget()
        self.content_stack.addWidget(self.upload_page)
        
        # 添加个人中心页面
        self.profile_page = ProfileWidget()
        self.content_stack.addWidget(self.profile_page)
        
        # 添加到主布局
        layout.addWidget(left_card)
        layout.addWidget(self.content_stack)
        
        # 绑定事件
        self.menu_list.currentRowChanged.connect(self.content_stack.setCurrentIndex)
        
        # 初始化分页控件
        self.page_info = BodyLabel(f"第 {self.current_page} 页 / 共 {self.get_total_pages()} 页")
        self.prev_btn = PushButton("上一页")
        self.prev_btn.setIcon(FIF.PAGE_LEFT)
        self.prev_btn.clicked.connect(self.prev_page)
        self.prev_btn.setEnabled(self.current_page > 1)  # 初始状态
        
        self.next_btn = PushButton("下一页")
        self.next_btn.setIcon(FIF.PAGE_RIGHT)
        self.next_btn.clicked.connect(self.next_page)
        self.next_btn.setEnabled(self.current_page < self.get_total_pages())  # 初始状态
        
        # 设置样式
        self.setStyleSheet("""
            QWidget {
                background-color: #f5f5f5;
            }
            
            CardWidget {
                border-radius: 8px;
                background-color: white;
                margin: 10px;
            }
            
            ListWidget {
                border: none;
                background-color: transparent;
            }
            
            ListWidget::item {
                padding: 10px;
                margin: 5px;
                border-radius: 5px;
            }
            
            ListWidget::item:selected {
                background-color: #e3f2fd;
                color: #1976d2;
            }
            
            ListWidget::item:hover {
                background-color: #f5f5f5;
            }
        """)

        
    def add_menu_item(self, text, icon):
        """添加菜单项"""
        item = QListWidgetItem(icon, f"  {text}")
        item.setSizeHint(QSize(item.sizeHint().width(), 50))
        self.menu_list.addItem(item)
        
    def create_exercise_page(self):
        """创建考题练习页面"""
        page = QWidget()
        layout = QVBoxLayout(page)
        layout.setSpacing(15)
        
        # 顶部信息区域
        top_card = CardWidget()
        top_layout = QHBoxLayout(top_card)
        
        # 标题和描述
        title_layout = QVBoxLayout()
        title = StrongBodyLabel("模拟试卷")
        title.setStyleSheet("""
            font-size: 28px;
            font-weight: bold;
            color: #1a73e8;
        """)
        
        description = BodyLabel("共20套模拟试卷，每套试卷包含单选、多选、判断、填空题")
        description.setStyleSheet("color: #666; font-size: 14px;")
        
        title_layout.addWidget(title)
        title_layout.addWidget(description)
        
        top_layout.addLayout(title_layout)
        layout.addWidget(top_card)
        
        # 试卷列表容器
        self.papers_container = QWidget()
        self.papers_layout = QVBoxLayout(self.papers_container)
        self.papers_layout.setSpacing(10)
        
        # 分页控制区域
        pagination_card = CardWidget()
        pagination_layout = QHBoxLayout(pagination_card)
        pagination_layout.setContentsMargins(20, 10, 20, 10)
        
        # 页码信息
        self.page_info = BodyLabel(f"第 {self.current_page} 页 / 共 {self.get_total_pages()} 页")
        self.prev_btn = PushButton("上一页")
        self.prev_btn.setIcon(FIF.PAGE_LEFT)
        self.prev_btn.clicked.connect(self.prev_page)
        self.prev_btn.setEnabled(self.current_page > 1)  # 初始状态
        
        self.next_btn = PushButton("下一页")
        self.next_btn.setIcon(FIF.PAGE_RIGHT)
        self.next_btn.clicked.connect(self.next_page)
        self.next_btn.setEnabled(self.current_page < self.get_total_pages())  # 初始状态
        
        pagination_layout.addStretch()
        pagination_layout.addWidget(self.prev_btn)
        pagination_layout.addWidget(self.page_info)
        pagination_layout.addWidget(self.next_btn)
        pagination_layout.addStretch()
        
        # 更新试卷列表
        self.update_papers_list()
        
        # 添加到主布局
        layout.addWidget(self.papers_container)
        layout.addWidget(pagination_card)
        
        # 使用滚动区域
        scroll = ScrollArea()
        scroll.setWidget(page)
        scroll.setWidgetResizable(True)
        scroll.setStyleSheet("""
            QScrollArea {
                border: none;
                background-color: transparent;
            }
            QScrollBar:vertical {
                width: 8px;
                background: transparent;
                margin: 0px;
            }
            QScrollBar::handle:vertical {
                background: #cdcdcd;
                min-height: 30px;
                border-radius: 4px;
            }
            QScrollBar::handle:vertical:hover {
                background: #a6a6a6;
            }
        """)
        
        return scroll

    def update_papers_list(self):
        """更新试卷列表"""
        # 清空现有内容
        for i in reversed(range(self.papers_layout.count())): 
            widget = self.papers_layout.itemAt(i).widget()
            if widget:
                widget.setParent(None)
                widget.deleteLater()
            
        # 计算当前页的试卷范围
        start_idx = (self.current_page - 1) * self.items_per_page
        end_idx = min(start_idx + self.items_per_page, self.total_papers)
        
        # 添加试卷卡片
        for i in range(start_idx + 1, end_idx + 1):
            paper_card = self.create_paper_card(i)
            self.papers_layout.addWidget(paper_card)
        print(self.current_page, start_idx,end_idx,"ccccc")

        # 更新页码信息
        self.page_info.setText(f"第 {self.current_page} 页 / 共 {self.get_total_pages()} 页")

        # 更新按钮状态
        self.prev_btn.setEnabled(self.current_page > 1)
        self.next_btn.setEnabled(self.current_page < self.get_total_pages())

        # 强制更新界面
        self.papers_container.repaint()
        
    def create_paper_card(self, paper_num):
        """创建试卷卡片"""
        paper_card = CardWidget()
        paper_card.setStyleSheet("""
            CardWidget {
                background: white;
                border-radius: 8px;
            }
            CardWidget:hover {
                background: #f8f9fa;
            }
        """)
        
        card_layout = QHBoxLayout(paper_card)
        card_layout.setContentsMargins(20, 15, 20, 15)
        
        # 试卷图标
        icon_label = QLabel()
        icon_label.setPixmap(FIF.BOOK_SHELF.icon().pixmap(QSize(32, 32)))
        card_layout.addWidget(icon_label)
        
        # 试卷信息
        info_layout = QVBoxLayout()
        paper_title = StrongBodyLabel(f"模拟试卷 {paper_num}")
        paper_title.setStyleSheet("font-size: 16px; font-weight: bold; color: #1a73e8;")
        
        paper_desc = BodyLabel("包含：单选题(20) 多选题(10) 判断题(10) 填空题(5)")
        paper_desc.setStyleSheet("color: #666; font-size: 13px;")
        
        info_layout.addWidget(paper_title)
        info_layout.addWidget(paper_desc)
        
        # 开始按钮
        start_btn = PrimaryPushButton("开始练习")
        start_btn.setIcon(FIF.PLAY)
        start_btn.clicked.connect(lambda: self.start_exercise(paper_num))
        start_btn.setFixedWidth(120)
        
        card_layout.addLayout(info_layout)
        card_layout.addStretch()
        card_layout.addWidget(start_btn)
        
        return paper_card
        
    def get_total_pages(self):
        """获取总页数"""
        return (self.total_papers + self.items_per_page - 1) // self.items_per_page
        
    def prev_page(self):
        """上一页"""
        if self.current_page > 1:
            self.current_page -= 1
            self.update_papers_list()
            print(f"当前页码: {self.current_page}")  # 调试信息
            
    def next_page(self):
        """下一页"""
        if self.current_page < self.get_total_pages():
            self.current_page += 1
            self.update_papers_list()
            print(f"当前页码: {self.current_page}")  # 调试信息
            
    def start_exercise(self, paper_num):
        """开始练习"""
        print(f"开始练习: 试卷编号 {paper_num}")  # 调试信息
        username = "test_user"
        self.exercise_window = ExamPaperWindow(username,paper_num)  # 确保传递正确的参数
        self.exercise_window.show()  # 显示答题面板

if __name__ == "__main__":
    app = QApplication([])
    window = DashboardWindow()
    window.show()
    app.exec()